package com.gy.hsim.bservice.framework.common.util;



import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @ClassName: HTTPSend 
 * @Description: 发送HTTP报文工具类 
 * @author Lee 
 * @date 2015-2-2 下午5:41:52
 */
public class HTTPSend {
	
	
	private static final Logger logger = LoggerFactory
			.getLogger(HTTPSend.class);
	
	public String sendRequest(String req, String request_url)
	 {
		String respJson = null;

		logger.info("request json : " + req);
		logger.info("request_url : " + request_url);
		HttpURLConnection con = null;
		DataOutputStream dos=null;
		ByteArrayOutputStream bOut = null;
		
//		try {
			URL serverUrl = null;
			// URLConnection con = null;
			
			byte[] reqData = null;
			try {
				long t0=System.currentTimeMillis();
				serverUrl = new URL(request_url);
			
				reqData = req.getBytes("UTF-8");
				con = (HttpURLConnection) serverUrl.openConnection();
				con.setRequestMethod("POST");
				con.setRequestProperty("Connection", "Close");
				con.setDoOutput(true);
				con.setDoInput(true);
				con.setUseCaches(false);
				con.setRequestProperty("Content-Type", "text/xml");
	
				con.setRequestProperty("Content-Length", reqData.length + "");
	
				dos = new DataOutputStream(con.getOutputStream());
				dos.write(reqData);

				int status = con.getResponseCode();
				if (status == HttpURLConnection.HTTP_OK) {
					
					InputStream input = con.getInputStream();
					
					bOut = new ByteArrayOutputStream(input.available());
		           
		            byte[] block = new byte[500];
		            int num = input.read(block);
		            while (num > 0) {
		                bOut.write(block, 0, num);
		                num = input.read(block);
		            }
		            input.close();
					
		            respJson = new String(bOut.toByteArray(), "UTF-8");

					Map<String,String> msgMap = new HashMap<String,String>();
					msgMap.put("msg_direction","发送报文");//报文请求方向
					msgMap.put("msg_body",respJson.toString());//报文体
					
					
					logger.info("HTTPSend.sendRequest() :" + request_url + "连接成功");
					
				} else {
					logger.error(" HTTP请求失败！HTTPConnection无响应！ ");
				}
	
			} catch (MalformedURLException e) {
				logger.error(" HTTPSend.sendRequest() MalformedURLException ", e);
			} catch (UnsupportedEncodingException e) {
				logger.error(" HTTPSend.sendRequest() UnsupportedEncodingException ", e);
			} catch (IOException e) {
				logger.error(" HTTPSend.sendRequest() IOException ", e);
			} finally {			
				try {				
					if(dos!=null){
						dos.flush();
						dos.close();
					}
				} catch (IOException e) {
					logger.error(" HTTPSend.sendRequest() IOException ", e);
				}
				
				try {
					if(bOut != null) {
						bOut.flush();
						bOut.close();
					}
				} catch (IOException e) {
					logger.error(" HTTPSend.sendRequest() IOException ", e);
				}	
				if (con != null) {
					con.disconnect();
				}
			}
			return respJson;
	}
}
